﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Rhapsody2Spotify.Lib;

namespace RhapsodyPlaylistExport
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
            cbxCountryCode.SelectedItem = "US";
            ReplaceEnviornmentalFolderPathsForAllControls();
        }


        #region Control Handlers
        private void SetDatabaseLocation_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Filter = "Rhapsody Databases (local3.seb)|local3.seb|" +
                             "All Files (*.*)|*.*";
                dlg.CheckFileExists = true;

                if (dlg.ShowDialog() == DialogResult.OK)
                    txtSourcedb.Text = dlg.FileName;
            }
        }


        private void SetOutputDirectory_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog dlg = new FolderBrowserDialog())
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    txtOutputDirectory.Text = dlg.SelectedPath.ToString();
                }
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            ReplaceEnviornmentalFolderPathsForAllControls();
            GetAllTracks();
            lblTracks.Text = "Complete.";

        }

        #endregion

        private void GetAllTracks()
        {
            var R2SC = new Rhapsody2SpotifyConverter(GetConnectionString(), cbxCountryCode.SelectedItem.ToString());

            int TrackCount = R2SC.TrackCount;
            pbrTracks.Maximum = TrackCount;
            R2SC.TrackProccessed += new EventHandler(incrementProgressBar);

            lblTracks.Text = string.Format("Tracks ({0})", TrackCount);
            lblTracks.Refresh();

            R2SC.Execute(txtOutputDirectory.Text);
        }

        void incrementProgressBar(object sender, EventArgs e)
        {
            pbrTracks.Increment(1);
        }

        private string GetConnectionString()
        {
            return string.Format(@"Data Source={0};FailIfMissing=true;", txtSourcedb.Text);
        }


        private void ReplaceEnviornmentalFolderPathsForAllControls()
        {
            txtSourcedb.Text = ReplaceEnviornmentalFolderPaths(txtSourcedb.Text);
            txtOutputDirectory.Text = ReplaceEnviornmentalFolderPaths(txtOutputDirectory.Text);
        }
        private string ReplaceEnviornmentalFolderPaths(string input)
        {
            return input.Replace("%appdata%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
                        .Replace("%desktop%", Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
        }

    }
}
